
import {Category} from "./model/mongo/book/category.entity";
import {Books} from "./model/mongo/book/books.entity";
import {Message} from "./model/mongo/tao/message.entity";
import {Connection, createConnection} from "typeorm";

export const Connect = [
    {
        provide: 'MongoBookConnectionToken',
        useFactory: async () =>
            await createConnection({
                type: 'mongodb',
                host: process.env.mongo_host,
                port: parseInt(process.env.mongo_port),
                database: 'book',
                entities: [__dirname + '/**/mongo/book/*.entity{.ts,.js}'],
            }),
    },
    {
        provide: 'MongoTaoConnectionToken',
        useFactory: async () =>
            await createConnection({
                type: 'mongodb',
                host: process.env.mongo_host,
                port: parseInt(process.env.mongo_port),
                database: 'tao',
                entities: [__dirname + '/**/mongo/tao/*.entity{.ts,.js}'],
            }),
    },
];

export const Model = [
    {
        provide: 'categoryModelToken',
        useFactory: (connection: Connection) => connection.getMongoRepository(Category),
        inject: ['MongoBookConnectionToken'],
    },
    {
        provide: 'booksModelToken',
        useFactory: (connection: Connection) => connection.getMongoRepository(Books),
        inject: ['MongoBookConnectionToken'],
    },
    {
        provide: 'messageModelToken',
        useFactory: (connection: Connection) => connection.getMongoRepository(Message),
        inject: ['MongoTaoConnectionToken'],
    },
];
